import { Injectable } from '@angular/core';

import { ConfigType } from './model/config.definition';

@Injectable()
export class ConfigService{
    config:any;
    ins: any = (<any>window);
    constructor() {
        //this.loadConfig(type, x => { this.config = JSON.parse(x); });
        this.loadConfig(this.ins.Env);
    }

    private loadConfig(type: string) {
      
        // let configUrl = `config/app.${type}.config.js`;
        // let xhr: XMLHttpRequest = new XMLHttpRequest();
        // xhr.overrideMimeType('application/json');
        // xhr.open('GET', configUrl, true);
        // xhr.onreadystatechange = function () {
        //     if (xhr.readyState === 4 && xhr.status === 200) {
        //         callback(xhr.responseText);
        //     }
        // }
        // xhr.send(null);
        switch (type) {
            case ConfigType.Dev:
                this.config = this.ins.DevConfig;
                break;
            case ConfigType.Release:
                this.config = this.ins.ReleaseConfig;
                break;
            case ConfigType.QA:
                this.config = this.ins.QAConfig;
                break;
            case ConfigType.Test:
                this.config = this.ins.TestConfig;
                break;
            default:
                this.config = {};
        }
    }

    getValueByKey(key: string): any {
        return this.config[key] || '';

    }
}